/**
 *  Copyright (C) 2008-2013  Telosys project org. ( http://www.telosys.org/ )
 *
 *  Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, Version 3.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *          http://www.gnu.org/licenses/lgpl.html
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.telosys.persistence.syntax;

import java.util.HashMap;

/***
SQL FUNCTIONS 

  AVG          : SELECT AVG(column) FROM Table    /  AVG(ALL Column)  /  AVG(DISTINCT column)
  COUNT        : SELECT COUNT(column) FROM Table  /  COUNT(ALL column)  /  COUNT(DISTINCT column)  /  COUNT(*)
  MAX          : SELECT MAX(column) FROM Table    /  MAX(ALL column)  /  MAX(DISTINCT column)
  MIN          : SELECT MIN(column) FROM Table    /  MIN(ALL column)  /  MIN(DISTINCT column)
  SUM          : SELECT SUM(column) FROM Table    /  SUM(ALL column)  /  SUM(DISTINCT column)
  EVERY (SQL3) : SELECT EVERY(column) FROM Table  /  EVERY(ALL column)  /  EVERY(DISTINCT column) 
  ANY  (=SOME) : WHERE  e1.sal > ANY (SELECT e2.sal FROM emp e2 WHERE e2.deptno = 100)  : same as "SOME"
  SOME (=ANY)  : WHERE  e1.sal > ANY (SELECT e2.sal FROM emp e2 WHERE e2.deptno = 100)  : same as "ANY"

  NULLIF SELECT col3 NULLIF(col1,col2) FROM table

***/
public class SqlSyntax 
{
	private static HashMap<String,Boolean> keyWordsMap = new HashMap<String,Boolean>() ;

	private static String[] keyWordsArray = null ;

	static {

		// keyWordsMap = new HashMap<String,Boolean>();

		String words[] = 
		{
		"AND", "ALL", "AVG", "AS", "ASC", "ANY",
		"BY", "BETWEEN", 
		"COUNT", "CASEWHEN", "CONVERT", "CAST", "CONCAT", "CALL", "COALESCE",
		"DELETE", "DISTINCT", "DESC", 
		"EXISTS", "EXCEPT", "EMPTY", "EVERY", "EXTRACT",
		"FALSE", "FROM", "FETCH", "FULL",
		"GROUP", 
		"HAVING",
		"INSERT", "IF", "INTO", "IFNULL", "IS", "IN", "INTERSECT", "INNER", "INTERVAL", 
		"JOIN",
		"LEFT", "LIKE", "LOWER",
		"MAX", "MIN", "MINUS", "MEMBER", "MOD", "MATCH",
		"NULL", "NOT", "NATURAL", "NEW",
		"ON", "ORDER", "OR", "OUTER", "OBJECT", "OF", "OVERLAPS", "OCTET_LENGTH",
		"PRIMARY", "POSITION",
		"RIGHT",
		"SELECT", "SET", "SUM", "SOME",
		"TO", "TRUE", "TRIM",
		"UPDATE", "UNIQUE", "UNION", "UNKNOWN", "UPPER",
		"VALUES", 
		"WHERE"
		};

		for (int i = 0; i < words.length; i++) 
		{
			keyWordsMap.put(words[i], true);
		}
		
		keyWordsArray = words ;
	}

	public static boolean isReservedWord(String word)
	{
		Boolean b = keyWordsMap.get( word.toUpperCase() ) ;
		return ( b != null );
	}

	public static String[] getReservedWords()
	{
		return keyWordsArray ;
	}
	
	public static int countParameters(String s)
	{
		if ( null == s ) return 0 ;
		
		boolean inLiteralString = false ;
        int count = 0 ;
        for ( int index = 0 ; index < s.length() ; index++ )
        {
            char c = s.charAt(index);
        	//System.out.println("CHAR : " + c );
            if ( c == '?' ) 
            {
                if ( inLiteralString != true ) 
                {
                	count++ ;
                }
            }
            if ( c == '\'' ) 
            {
            	//System.out.println("QUOTE : " + inLiteralString );
            	inLiteralString = ( ! inLiteralString ) ;
            }
        }
        return count ;
	}
}
